<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input v-model="categoryName" placeholder="请输入分类名称" clearable size="small" prefix-icon="el-icon-search" style="margin-bottom: 20px"/>
        </div>
        <div class="head-container">
          <el-tree :data="cateOptions" :props="defaultProps" :expand-on-click-node="false" :filter-node-method="filterNode"
                   ref="tree" default-expand-all highlight-current @node-click="handleNodeClick"/>
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
        <!-- 搜索工作栏 -->
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="题目名称" prop="subjectName">
            <el-input v-model="queryParams.subjectName" placeholder="请输入题目名称" clearable @keyup.enter.native="handleQuery"/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <!-- 操作工具栏 -->
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                      v-hasPermi="['exam:subject-short-answer:create']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                      v-hasPermi="['exam:subject-short-answer:export']">导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <!-- 列表 -->
        <el-table v-loading="loading" :data="list">
          <el-table-column label="题目名称" align="center" prop="subjectName" :show-overflow-tooltip="true"/>
          <el-table-column label="分值" align="center" prop="score" width="50"/>
          <el-table-column label="参考答案" align="center" prop="answer" :show-overflow-tooltip="true"/>
          <el-table-column label="难度等级" align="center" prop="level" >
            <template slot-scope="scope">
              <el-rate :value="scope.row.level" disabled></el-rate>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTime" width="150">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <!-- <el-table-column label="修改人" align="center" prop="modifier" />
          <el-table-column label="修改时间" align="center" prop="modifyDate" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.modifyDate) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="删除标记 0:正常;1:删除" align="center" prop="delFlag" />
          <el-table-column label="系统编号" align="center" prop="applicationCode" />
          <el-table-column label="租户编号" align="center" prop="tenantCode" /> -->
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['exam:subject-short-answer:update']">修改</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['exam:subject-short-answer:delete']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize" @pagination="getList"/>
      </el-col>
    </el-row>
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row  :gutter="15">
          <el-col :span="12" :xs="24">
            <el-form-item label="分值" prop="score">
              <el-input-number size="medium" v-model="form.score" :min="1" placeholder="请填写分值" :step="1" :step-strictly="true"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12" :xs="24">
            <el-form-item label="难度等级" prop="level">
              <el-rate v-model="form.level" show-text :texts="texts"></el-rate>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="题目分类" prop="categoryId">
           <treeselect v-model="form.categoryId" :options="cateOptions" :show-count="true" :clearable="false"
                       placeholder="请选择归属分类" :normalizer="normalizer"/>
        </el-form-item>
        <el-form-item label="题目名称" prop="subjectName">
          <tinymce  v-model="form.subjectName"/>
        </el-form-item>
        <el-form-item label="参考答案" prop="answer">
          <tinymce  v-model="form.answer"/>
        </el-form-item>
        <el-form-item label="解析" prop="analysis">
          <tinymce  v-model="form.analysis"/>
        </el-form-item>
        <!--<el-form-item label="创建时间" prop="createDate">
          <el-date-picker clearable v-model="form.createDate" type="date" value-format="timestamp" placeholder="选择创建时间" />
        </el-form-item>
         <el-form-item label="修改人" prop="modifier">
          <el-input v-model="form.modifier" placeholder="请输入修改人" />
        </el-form-item>
        <el-form-item label="修改时间" prop="modifyDate">
          <el-date-picker clearable v-model="form.modifyDate" type="date" value-format="timestamp" placeholder="选择修改时间" />
        </el-form-item> 
        <el-form-item label="删除标记 0:正常;1:删除" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标记 0:正常;1:删除" />
        </el-form-item>
        <el-form-item label="系统编号" prop="applicationCode">
          <el-input v-model="form.applicationCode" placeholder="请输入系统编号" />
        </el-form-item>
        <el-form-item label="租户编号" prop="tenantCode">
          <el-input v-model="form.tenantCode" placeholder="请输入租户编号" />
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { createSubjectShortAnswer, updateSubjectShortAnswer, deleteSubjectShortAnswer, getSubjectShortAnswer, getSubjectShortAnswerPage, exportSubjectShortAnswerExcel } from "@/api/exam/subjectShortAnswer";
import { getSimpleList } from "@/api/examination/subjectCategory";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import tinymce from '@/components/tinymce';
export default {
  name: "SubjectShortAnswer",
  components: {
    Treeselect,
    tinymce
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 简答题列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        categoryId:null,
        subjectName: null,
      },
      // 表单参数
      form: {},
      //难度等级
      texts:['容易','偏易','适中','偏难','困难'],
      // 表单校验
      rules: {
        categoryId: [{ required: true, message: "题目分类不能为空", trigger: "blur" }],
        subjectName: [{ required: true, message: "题目名称不能为空", trigger: "blur" }],
        score: [{ required: true, message: "分值不能为空", trigger: "blur" }],
        level: [{ required: true, message: "难度等级不能为空", trigger: "blur" }],
      },
      defaultProps: {
        children: "children",
        label: "categoryName"
      },
      cateOptions:[],
      categoryName:undefined,
    };
  },
  watch: {
    // 根据名称筛选分类
    categoryName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getTreeselect();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getSubjectShortAnswerPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = parseInt(response.data.total);
        this.loading = false;
      });
    },
    /** 查询分类下拉树结构 */
    getTreeselect() {
      getSimpleList().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          response.data[i].id = parseInt(response.data[i].id);
          response.data[i].parentId = parseInt(response.data[i].parentId);
        }
        // 处理 deptOptions 参数
        this.cateOptions.push(...this.handleTree(response.data, "id"));
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.categoryName.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.categoryId = data.id;
      this.getList();
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        categoryId: undefined,
        subjectName: undefined,
        answer: undefined,
        score: undefined,
        analysis: undefined,
        level: undefined,
        createDate: undefined,
        modifier: undefined,
        modifyDate: undefined,
        delFlag: undefined,
        applicationCode: undefined,
        tenantCode: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.categoryId = this.queryParams.categoryId;
      this.open = true;
      this.title = "添加简答题";
      
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getSubjectShortAnswer(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改简答题";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateSubjectShortAnswer(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createSubjectShortAnswer(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除简答题编号为"' + id + '"的数据项?').then(function() {
          return deleteSubjectShortAnswer(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有简答题数据项?').then(() => {
          this.exportLoading = true;
          return exportSubjectShortAnswerExcel(params);
        }).then(response => {
          this.$download.excel(response, '简答题.xls');
          this.exportLoading = false;
        }).catch(() => {});
    },
    // 格式化课程树的下拉框
    normalizer(node) {
      return {
        id: node.id,
        label: node.categoryName,
        children: node.children
      }
    },
  }
};
</script>
